package kr.co.redcircle.broker.security;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;

public class KeyCollection {

	
	private static final String INSTANCE_PKCS12 = "PKCS12";
	private static final String INSTANCE_JKS = "JKS";
	public static final String TRUST_KEY_STORE = "trust";
	public static final String KEY_STORE = "key";
	
	private KeyStore trustStore;
	private KeyStore keyStroe;
	TrustManagerFactory trustManagerFactory;
	KeyManagerFactory keyManagerFactory;
	
	public KeyCollection() {
		String alg = KeyManagerFactory.getDefaultAlgorithm();
		try {
			
			// setup trust stroe
			trustStore = KeyStore.getInstance(INSTANCE_JKS);
			trustManagerFactory = TrustManagerFactory.getInstance(alg);
			
			// setup key stroe
			keyStroe = KeyStore.getInstance(INSTANCE_PKCS12);
			keyManagerFactory = KeyManagerFactory.getInstance(alg);
			
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (KeyStoreException e) {
			e.printStackTrace();
		}
	}

	public KeyStore getTrustStore() {
		return this.trustStore;
	}
	
	public KeyStore getKeyStore() {
		return  this.keyStroe;
	}

	public void addTrustStore(String keyFile, String password){
		loadStore(this.trustStore, keyFile, password);
	}

	public void setupKeyStore(String keyFile, String password){
		loadStore(this.keyStroe, keyFile, password);
	}

	private void loadStore(KeyStore keyStore, String keyFile, String password){
		FileInputStream fis;
		try {
			fis = new FileInputStream(keyFile);			
			keyStore.load(fis, password.toCharArray());
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (CertificateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void setupKeyCollection(char[] keyStorePassword){
		
		try {
			// setup trust key store;
			trustManagerFactory.init(trustStore);
			// setup key store;
			keyManagerFactory.init(keyStroe, keyStorePassword);
			
		} catch (KeyStoreException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnrecoverableKeyException e) {
			e.printStackTrace();
		}
	}

	public void init(String password) {
		setupKeyCollection(password.toCharArray());
	}

	public KeyManager[] getKeyManagers() {
		return this.keyManagerFactory.getKeyManagers();
	}

	public TrustManager[] getTrustManagers() {
		return this.trustManagerFactory.getTrustManagers();
	}
	
}
